<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/17
 * Time: 11:02
 */

namespace App\CronTask;


use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\HttpClient\HttpClient;

class LiveBattleCron extends AbstractCronTask
{
    public static function getRule(): string
    {
        // TODO: Implement getRule() method.
        //return '@daily';
        return '*/1 * * * *';
    }
    public static function getTaskName(): string
    {
        // TODO: Implement getTaskName() method.
        return 'LiveBattle';
    }
    static function run(\swoole_server $server, int $taskId, int $fromWorkerId, $flags = null)
    {
        $matchTables = ['siam_lolmatchs','siam_dotamatchs'];
        $path = [UrlEnums::LOL_LIVE_BATTLE_PATH,UrlEnums::DOTA_LIVE_BATTLE_PATH];
        $games = ['lol','dota'];
        $tables = ['siam_lollive_battle','siam_dotalive_battle'];
        for ( $i=0;$i<count($path);$i++ ){
            MysqlPool::invoke(function (MysqlObject $db)use($path,$i,$matchTables,$tables){
                $res = $db->where('status',2)->getColumn($matchTables[$i],'battle_ids');
                $battle_ids = [];
                foreach ($res as $item){
                    $arr = json_decode($item,true);
                    $battle_ids = array_merge($battle_ids,$arr);
                }
                foreach ( $battle_ids as $battle_id ){
                    list($s1, $s2) = explode(' ', microtime());
                    $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                    $liveBattle = $db->where('battle_id',$battle_id)->getOne($tables[$i]);//战役不存在，插入
                    if( !$liveBattle ){
                        $url = UrlEnums::FJURL.$path[$i]."?battle_id={$battle_id}&version=2";
                        $client = new HttpClient($url);
                        $option = [
                            'path'      => $path[$i],
                            'api_time'  => $microtime
                        ];
                        $header = Auth::getInstance()->header($option);
                        $client->setHeaders($header,true,false);
                        $response = $client->get();
                        $content = $response->getBody();
                        $data = json_decode($content,true);
                        if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
                            $data['data']['economic_diff'] = json_encode($data['data']['economic_diff'] ?? []);
                            $data['data']['xp_diff'] = json_encode($data['data']['xp_diff'] ?? []);

                            if( isset($data['data']['player_stats']) ){
                                $data['data']['player_stats'] = json_encode($data['data']['player_stats']);
                            }
                            if( isset($data['data']['players']) ){
                                $data['data']['player_stats'] = json_encode($data['data']['players']);
                                unset($data['data']['players']);
                            }
                            $data['data']['team_stats'] = json_encode($data['data']['team_stats']);
                            $res = $db->insert($tables[$i],$data['data']);
                        }
                    }
                }
            });
        }
    }
}